from ftplib import FTP

def anonymous_ftp_login(host, port=2121):
    # 连接到FTP服务器，并指定端口号
    ftp = FTP()
    ftp.encoding = 'UTF-8'  # 设置编码
    try:
        ftp.connect(host, port)
        print("Connecting to server...")
        
        # 尝试匿名登录
        ftp.login(user='anonymous', passwd='')
        print("Successfully logged in anonymously.")
    except Exception as e:
        print(f"Login failed: {e}")
        return None
    
    return ftp

# 用你的FTP服务器地址替换'your_ftp_server_address'
ftp = anonymous_ftp_login('192.168.31.9', 2121)

if ftp:
    # 登录成功后，你可以执行FTP命令，例如列出目录：
    ftp.retrlines('LIST /0Music')
    print('---------------------------')
    ftp.retrlines('LIST /0Music/＠陈慧娴《经典重逢》')
    # 不要忘记关闭连接
    ftp.quit()